package com.meepo.feed.feed

import android.os.Bundle
import android.util.Log
import android.view.View
import com.meepo.basic.components.eventbus.Event
import com.meepo.basic.components.view.MeepoFragment
import com.meepo.framework.design.viewpager.adapter.DefaultFragmentStatePagerAdapter
import com.meepo.basic.router.RouterManager
import com.meepo.feed.API.Companion.FeedCategory_Follow
import com.meepo.feed.API.Companion.FeedCategory_Suggest
import com.meepo.feed.API.Companion.FeedCategory_Video
import com.meepo.feed.R
import com.meepo.framework.design.tabs.ViewPagerHelper
import com.meepo.framework.design.tabs.buildins.commonnavigator.CommonNavigator
import com.meepo.framework.design.tabs.sample.navadapters.ColorTransitionNavigatorAdapter
import com.meepo.framework.design.tabs.sample.TextTab
import kotlinx.android.synthetic.main.feed_cat_fragment.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @date  2020/11/13 11:34 AM
 * @version 1.0
 */
class FeedCatFragment : MeepoFragment() {

    override fun initData() {

    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    fun onEvent(event: Event) {
        Log.e("FeedCatFragment ", event.toString())
    }

    override fun enableEventBus(): Boolean = true

    override fun initView(view: View, savedInstanceState: Bundle?) {

        search_iv.setOnClickListener {
            RouterManager.FeedModule.routeToSearch(requireContext())
        }

        val tabs = listOf("推荐", "关注", "视频").map {
            TextTab(
                it, it,
                selectedTextColor = resources.getColor(R.color.primary_color),
                unSelectedTextColor = resources.getColor(R.color.primary_text_color),
                textSize = 18f
            )
        }.toList()

        val mFragments = listOf(
            FeedListFragment.instance(category = FeedCategory_Suggest),
            FeedListFragment.instance(category = FeedCategory_Follow),
            FeedListFragment.instance(category = FeedCategory_Video)
        )

        val commonNavigator =
            CommonNavigator(requireContext(), ColorTransitionNavigatorAdapter(tabs) { index -> view_pager.currentItem = index })
        magic_indicator.navigator = commonNavigator
//        val titleContainer = commonNavigator.titleContainer // must after setNavigator

//        titleContainer.showDividers = LinearLayout.SHOW_DIVIDER_MIDDLE
//        titleContainer.dividerPadding = UIUtil.dip2px(requireContext(), 15.0)
//        titleContainer.dividerDrawable = resources.getDrawable(R.drawable.simple_splitter)

        view_pager.adapter = DefaultFragmentStatePagerAdapter(this.childFragmentManager, mFragments)

        ViewPagerHelper.bind(magic_indicator, view_pager)
    }

    override fun setUpContentLayout(): Int = R.layout.feed_cat_fragment

}